<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Controller\AdminController;

class AdvController extends AdminController {

    function _initialize() {
        parent::_initialize();
        $this->assign('action', 'adv');
        $this->assign('sys', 'website'); // 数据集
    }
    //put your code here
    //广告位
    public function index() {
        if (I('nav_title')) {
            $where['ap_name'] = array('like', '%' . I('nav_title') . '%');
        }
        $navi = M('advPosition');
        $count = $navi->where($where)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $list = $navi->order('ap_id asc')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('pages', $show); // 赋值分页输出
        $this->display();
    }

    //广告位添加
    public function add() {
        if (IS_POST) {
            $data['ap_name'] = I('ap_name');
            $data['ap_intro'] = I('ap_intro');
            $data['ap_class'] = I('ap_class');
            switch ($data['ap_class']) {
                case 0:$data['ap_width'] = I('ap_width_media');
                    $data['default_content'] = I('default_content');
                    break;
                case 1:$data['ap_width'] = I('ap_width_word');
                    $data['default_word'] = I('default_word');
                    break;
                case 3:$data['ap_width'] = I('ap_width_media');
                    $data['default_content'] = I('default_content');
                    break;
            }
            $data['ap_display'] = I('ap_display');
            $data['is_use'] = I('is_use');
            $data['ap_height'] = I('ap_height');
            $re = M('advPosition')->add($data);
            if ($re) {
                
                //日志记录
                $logobj = new Logs();
                $html = I('ap_name');
                $logobj->write(array("管理员:" . session('manager_auth.username'), "添加广告位", $html));
            
                $this->success('广告位增加成功！');
            } else {
                $this->error('广告位增加失败！');
            }
        }


        $this->display();
    }

    //广告位修改
    public function edit() {

        $advPosition = M('advPosition');
        $where['ap_id'] = I('get.ap_id');
        if (IS_POST) {
            $data['ap_name'] = I('ap_name');
            $data['ap_intro'] = I('ap_intro');
            $data['ap_class'] = I('ap_class');
            switch ($data['ap_class']) {
                case 0:$data['ap_width'] = I('ap_width_media');
                    $data['default_content'] = I('default_content');
                    break;
                case 1:$data['ap_width'] = I('ap_width_word');
                    $data['default_content'] = I('default_word');
                    break;
                case 3:$data['ap_width'] = I('ap_width_media');
                    $data['default_content'] = I('default_content');
                    break;
            }
            $data['ap_display'] = I('ap_display');
            $data['is_use'] = I('is_use');
            $data['ap_height'] = I('ap_height');
            $re = $advPosition->where($where)->save($data);
            if ($re) {
                //日志记录
                $logobj = new Logs();
                $html = I('ap_name');
                $logobj->write(array("管理员:" . session('manager_auth.username'), "修改广告位", $html));
                $this->success('广告位修改成功！');
            } else {
                $this->error('广告位修改失败！');
            }
        }
        $ap = $advPosition->where($where)->find();

        switch ($ap['ap_class']) {
            case 0:$ap_class[0] = "selected='selected'";
                break;
            case 1:$ap_class[1] = "selected='selected'";
                break;
            case 2:$ap_class[2] = "selected='selected'";
                break;
            case 3:$ap_class[3] = "selected='selected'";
                break;
        }

        switch ($ap['is_use']) {
            case 0:$is_use[0] = 'checked="checked"';
                break;
            case 1:$is_use[1] = 'checked="checked"';
                break;
        }

        switch ($ap['ap_display']) {
            case 1:$ap_display[1] = 'checked="checked"';
                break;
            case 2:$ap_display[2] = 'checked="checked"';
                break;
        }

        $this->assign('ap', $ap);
        $this->assign('ap_class', $ap_class);
        $this->assign('is_use', $is_use);
        $this->assign('ap_display', $ap_display);
        $this->display();
    }

    //广告位删除
    public function del() {
        $where['ap_id'] = array('in', I('ap_id'));
        $re = M('advPosition')->where($where)->delete();

        if ($re) {
            M('adv')->where($where)->delete();
            //日志记录
            $logobj = new Logs();
            $html = I('ap_id');
            $logobj->write(array("管理员:" . session('manager_auth.username'), "删除广告位", $html));
            
            $this->success('删除广告位成功！');
        } else {
            $this->error('删除广告位失败！');
        }
    }

    //广告列表
    public function adv() {
        if (I('nav_title')) {
            $where['adv_title'] = array('like', '%' . I('nav_title') . '%');
        }
        $ap_id = I('ap_id');
        $navi = M('adv');
        $where['ap_id'] = $ap_id;
        $count = $navi->where($where)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $list = $navi->where($where)->order('adv_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
//        p($list);die;
        $this->assign('ap_id', $ap_id);
        $this->assign('pages', $show); // 赋值分页输出
        $this->display();
    }

    //广告添加
    public function advAdd() {

        $ap_id = I('ap_id');
        $navi = M('adv');
        if (IS_POST) {

            $data['adv_title'] = I('adv_name');
            $data['ap_id'] = I('ap_id');
            $data['other'] = I('other');
            $data['adv_pc_pic'] = I('adv_pc_pic');
            $data['adv_pc_url'] = I('adv_pc_url');
            $data['adv_mb_pic'] = I('adv_mb_pic');
            $data['adv_mb_url'] = I('adv_mb_url');
            $data['adv_start_date'] = strtotime(I('adv_start_time'));
            $data['adv_end_date'] = strtotime(I('adv_end_time'));
            $data['slide_sort'] = I('slide_sort');
            
            $re = $navi->add($data);
            if ($re) {
                //日志记录
                $logobj = new Logs();
                $html = I('adv_name');
                $logobj->write(array("管理员:" . session('manager_auth.username'), "添加广告", $html));
                $this->success('增加广告位成功！');
            } else {
                $this->error('增加失败！');
            }
        }
        $where['ap_id'] = $ap_id;
        //查询当前广告的类别
        $ap_type = M('advPosition')->where($where)->getField('ap_class');
        $this->assign('ap_type', $ap_type); // 赋值分页输出
        $advPosition = M('advPosition')->select();
        $this->assign('advPosition', $advPosition);
        $this->display();
    }

    //广告修改
    public function advEdit() {
        $Adv = M('adv');
        $where['adv_id'] = I('get.adv_id');
        if (IS_POST) {
            $data['adv_title'] = I('adv_name');
            $data['ap_id'] = I('ap_id');
            $data['other'] = I('other');
            $data['adv_pc_pic'] = I('adv_pc_pic');
            $data['adv_pc_url'] = I('adv_pc_url');
            $data['adv_mb_pic'] = I('adv_mb_pic');
            $data['adv_mb_url'] = I('adv_mb_url');
            $data['adv_start_date'] = strtotime(I('adv_start_time'));
            $data['adv_end_date'] = strtotime(I('adv_end_time'));
            $data['slide_sort'] = I('slide_sort');

            $re = $Adv->where($where)->save($data);
            if ($re) {
                //日志记录
                $logobj = new Logs();
                $html = I('adv_name');
                $logobj->write(array("管理员:" . session('manager_auth.username'), "编辑广告", $html));
                $this->success('修改广告位成功！');
            } else {
                $this->error('修改失败！');
            }
        }
        $Position = M('advPosition')->select();
        $this->assign('advPosition', $Position);

        $adv = $Adv->where($where)->find();

        $this->assign('adv', $adv);
        $this->display();
    }

    //广告删除
    public function advDel() {
        $where['adv_id'] = I('data');
        $re = M('adv')->where($where)->delete();
        if ($re) {
            //日志记录
            $logobj = new Logs();
            $html = I('data');
            $logobj->write(array("管理员:" . session('manager_auth.username'), "删除广告", $html));
            $this->success('删除广告成功！');
        } else {
            $this->error('删除广告失败！');
        }
    }

}
